import xyz.migle.base.*;
import xyz.migle.base.task.*;
class KotJob():Job {
    var author: String? = null;
    var jobId: String? = null;
    var jobName: String? = null;
    var jobDescribe: String? = null;
    var period: String? = null;
    var batch: String? = null;
    var tasks: MutableList<Task<*>> = ArrayList()

    init {
        println("First initializer block that prints ${jobName}")
    }

//    fun<T> task(task: () -> Task<T>): T {
//        tasks.add(task());
//        return task().exec();
//    }
    fun<T> task(task: () -> Task<T>): T {
        tasks.add(task());
        return task().exec();
    }
    //默认顺序执行，如果要有条件分支，需要自行覆盖此方法
    //TODO:如果是有条件分支出错怎么处理？？？？这种结构应该是处理不了
    //并结构如何处理？  a->(b,c)->d,bc可以同时执行，将（b,c）包装成一个任务，转换成线性结构
    fun onExecute(): KotJob {
        tasks.forEach{ t -> t.exec() }

        return this
    }

    internal var _success: (String) -> Unit = {}
    internal var _fail: (Throwable) -> Unit = {}

    fun onSuccess(onSuccess: (String) -> Unit) {
        _success = onSuccess
    }

    fun onFail(onError: (Throwable) -> Unit) {
        _fail = onError
    }
    //Kotlin 提供了 invoke 约定，可以让对象像函数一样直接调用
}

fun job(create: KotJob.() -> Unit): KotJob {
    val job = KotJob();
    job.create()
    println("name is :${job.jobName}")
    return job
}


fun sql(f: SQLTask.() -> Unit): SQLTask {
    val task = SQLTask()
    task.f()
    return task
}

fun mysql(f: SQLTask.() -> Unit): SQLTask {
    val task = SQLTask()
    task.type = "mysql"
    task.driverClassName="com.mysql.cj.jdbc.Driver"
    task.url = "jdbc:mysql://10.10.10.100:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"
    task.user ="root"
    task.pwd = "admin"
    task.f()
    return task
}


